<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/21
 * Time: 17:30
 */

/**
 * Class upload
 * @User: yfl
 * @Time:
 * @description:切割文件长传
 * 1.建立上传文件夹
 * 2.把文件从上传临时目录移动到上传文件夹
 * 3.所有的文件块上传完成后，进行文件合成
 * 4.删除文件夹
 * 5.返回上传后的文件路径
 */
class upload
{
    private $filePath = './saveUploadFile';//上传目录
    private $tmpPath; //php临时目录
    private $blobNum; // 第几个文件块
    private $totalBlobNum; //文件块数
    private $fileName;//文件名

    public function __construct($tmpPath, $blobNum, $totalBlobNum, $fileName)
    {
        $this->tmpPath = $tmpPath;
        $this->blobNum = $blobNum;
        $this->totalBlobNum = $totalBlobNum;
        $this->fileName = $fileName;
        $this->moveFile();
        $this->fileMerge();
    }

    /**
     * @User: yfl
     * @Time: 2017年9月21日 17:54:02
     * @description: 判断是否是最后一块，如果是则进行文件合成并且删除文件块
     */
    private function fileMerge()
    {
        if ($this->blobNum == $this->totalBlobNum) {
            $blob = '';
            for ($i = 1; $i <= $this->totalBlobNum; $i++) {
                $blob .= file_get_contents($this->filePath . '/' . $this->fileName . '__' . $i);
            }
            file_put_contents($this->filePath . '/' . $this->fileName, $blob);
            $this->deleteFileBlob();
        }
    }

    /**
     * @User: yfl
     * @Time: 2017年9月21日 17:53:09
     * @description:合并完成后删除文件块
     */
    private function deleteFileBlob()
    {
        for ($i = 1; $i <= $this->totalBlobNum; $i++) {
            @unlink($this->filePath . '/' . $this->fileName . '__' . $i);
        }

    }

    /**
     * @User: yfl
     * @Time: 2017年9月21日 17:31:24
     * @description:上传移动文件快
     */
    private function moveFile()
    {
        $this->makeDir();
        $filename = $this->filePath . '/' . $this->fileName . '__' . $this->blobNum;
        move_uploaded_file($this->tmpPath, $filename);
    }

    /**
     * @User: yfl
     * @Time: 2017年9月21日 17:59:00
     * @description: 返回结果
     */
    public function apiReturn()
    {
        if ($this->blobNum == $this->totalBlobNum) {
            if (file_exists($this->filePath . '/' . $this->fileName)) {
                $data['code'] = 2;
                $data['msg'] = 'success';
                $data['file_path'] = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['DOCUMENT_URI']) . str_replace('.', '', $this->filePath) . '/' . $this->fileName;
            }
        } else {
            if (file_exists($this->filePath . '/' . $this->fileName . '__' . $this->blobNum)) {
                $data['code'] = 1;
                $data['msg'] = 'waiting for all';
                $data['file_path'] = '';
            }
            header('Content-type: application/json');
            echo json_encode($data);
        }

    }

    /**
     * @return bool
     * @User: yfl
     * @Time: 2017年9月21日 17:11:54
     * @description: 创建文件目录
     */
    private function makeDir()
    {
        if (!file_exists($this->filePath)) {
            return mkdir($this->filePath, 0777);
        }
    }
}

//实例化并获取系统变量传参
$upload = new upload($_FILES['file']['tmp_name'], $_POST['blob_num'], $_POST['total_blob_num'], $_POST['file_name']); //调用方法，返回结果 $upload->apiReturn();
